        <?php
  class Promotion extends Admin_Controller{
      function Promotion(){
          parent::Admin_Controller();
          $this->load->model('Promotion_m');
      }
      function index(){
        $this->list_promotion();    
      }
    function list_promotion(){
        is_admin();
        $this->_data['page_title'] = "Liste de promotion";
        $per_page=$this->uri->segment(3);
        $offset=$this->uri->segment(4);
        if($per_page==""){
            $per_page=10;
        }
        if($offset==""){
            $offset=0;
        }
        $config['base_url']=base_url().'index.php/promotion/list_promotion/'.$per_page.'/';
        $config['per_page']='10';
        $config['uri_segment']=4;
        $config['total_rows']=$this->db->count_all('promotion');
        
         $config['full_tag_open'] = "<div id='containpage'>";
        $config['full_tag_close'] = '</div>';
        $config['cur_tag_open'] = '<span id="pageactive"> ';
        $config['cur_tag_close'] = ' </span>';
        $config['next_link'] = '&gt;&gt;';
        $config['prev_link'] = '&lt;&lt;';
        
        if($config['total_rows']==$offset) $offset=$offset-$per_page;
         $this->_data['perpage']=$config['per_page'];
         $this->_data['offset']=$offset; 
        $this->_data['query']=$this->Promotion_m->get_all_promotion($config['per_page'],$offset);
        $this->pagination->initialize($config);
        $this->_data['pagination']=$this->pagination->create_links();
        $this->_data['total']=$this->db->count_all('promotion');
        $this->display_view('promotion/promotion');
    }
     function add_promotion(){
        is_admin();
        $this->_data['page_title'] = "Ajouter promotion";
          $config = array(
            array(
            'field'   => 'pct',
            'label'   => 'Pourcentage',
            'rules'   => 'required'
            ),
            array(
            'field'   => 'code',
            'label'   => 'Code',
            'rules'   => 'required'
            ),
             array(
            'field'   => 'qty', 
            'label'   => 'Quantite',
            'rules'   => 'required'
            )
              );
         $this->form_validation->set_rules($config);
          if($this->input->post('submit')){  
                      if($this->form_validation->run()){ 
                               $data = array(
                            'pct'=>$this->input->post('pct'),
                            'code'=>$this->input->post('code'),
                            'qty'=>$this->input->post('qty')
                              );

              $query = $this->Promotion_m->add_promotion($data);           
                 if($query==1)    redirect(site_url('promotion/list_promotion'));    
                 else {
                      $this->_data['erreur']="Erreur lors de la modification !";
                     $this->display_view('promotion/add_promotion'); 
                         }
                  
                        
               }  else   $this->display_view('promotion/add_promotion');   
         }    else   $this->display_view('promotion/add_promotion');   
    }
     function update_promotion($id,$perpage,$offset){
        is_admin();
        $this->_data['page_title'] = "Mise à jour de la promotion";
         $promotion=$this->Promotion_m->get_promotion($id); 
         $this->_data['promotion']=$promotion;  
         $this->_data['perpage']=$perpage;
         $this->_data['offset']=$offset;    
          $config = array(
            array(
            'field'   => 'pct',
            'label'   => 'Pourcentage',
            'rules'   => 'required'
            ),
            array(
            'field'   => 'code',
            'label'   => 'Code',
            'rules'   => 'required'
            ),
             array(
            'field'   => 'qty', 
            'label'   => 'Quantite',
            'rules'   => 'required'
            )
              );
           $this->form_validation->set_rules($config);
          if($this->input->post('submit')){  
                   if($this->form_validation->run()){ 
                               $data = array(
                            'pct'=>$this->input->post('pct'),
                            'code'=>$this->input->post('code'),
                            'qty'=>$this->input->post('qty')
                              );

              $query=$this->Promotion_m->update_promotion($data,$id);           
                 if($query==1) redirect(site_url('promotion/list_promotion'.'/'.$perpage.'/'.$offset));    
                 else {
                      $this->_data['erreur']="Erreur lors de la modification !";
                     $this->display_view('promotion/update_promotion'); 
                         }
                  
                        
               }  else  $this->display_view('promotion/update_promotion'); 
         }    else   $this->display_view('promotion/update_promotion'); 
    }
    function delete_promotion($id,$perpage,$offset){ 
         is_admin(); 
          $query=$this->Promotion_m->delete_promotion($id);  
             if($query==1)    redirect(site_url('promotion/list_promotion'.'/'.$perpage.'/'.$offset)); 
         }
  }
?>
